package org.microblog.dao.impl;

import org.microblog.dao.BaseDao;
import org.microblog.dao.UserDao;
import org.microblog.entity.User;
import org.microblog.util.DatabaseUtil;
import org.microblog.util.DateUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl extends BaseDao implements UserDao {
    public UserDaoImpl(Connection conn){
        super(conn);
    }
    @Override
    public User findUser(String userId, String password) throws SQLException{
        ResultSet rs = null;
        User user = null;
        //根据登录账户和密码查找匹配用户
        String sql = "select * from user where userId=? and password=?";
        try{
            rs = this.executeQuery(sql, userId,password);
            if(rs.next()){
                user = new User();
                user.setId(rs.getInt("id"));
                user.setUserId(rs.getString("userId"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                user.setImage(rs.getString("image"));
                user.setAddress(rs.getString("address"));
                user.setSign(rs.getString("sign"));
                user.setMysite(rs.getString("mysite"));
                user.setBirthday(rs.getDate("birthday"));
                user.setEmail(rs.getString("email"));
                user.setName(rs.getString("name"));
                user.setCreatetime(rs.getDate("createtime"));
                user.setIspublic(rs.getString("ispublic"));
            }
        }catch (SQLException e) {
            e.printStackTrace();
            throw e;
        } finally {
            DatabaseUtil.closeAll(null, null, rs);
        }
        return user;
    }

    @Override
    public int updateUser(User user) throws SQLException {
        String sql = "update user set password=?,email=?,nickname=?,name=?, image=?, address=?, mysite=?, sign=? , ispublic=? where id=?";
        int result = 0;
        try {
            result = this.executeUpdate(sql,user.getPassword(),user.getEmail(),user.getNickname(),user.getName(), user.getImage(),user.getAddress(),user.getMysite(),user.getSign(),user.getIspublic(), user.getId());
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    @Override
    public int findUserByid(String userId) throws SQLException {
        ResultSet rs = null;
        int count =0;
        //根据登录账户和密码查找匹配用户
        String sql = "select count(*) from user where userId=? ";
        try{
            rs = this.executeQuery(sql, userId);
            while (rs.next()) {
                count = rs.getInt("count(*)");
            }
        }catch (SQLException e) {
            e.printStackTrace();
            throw e;
        } finally {
            DatabaseUtil.closeAll(null, null, rs);
        }
        return count;
    }

    @Override
    public int register(User user) throws SQLException {
        String sql = "insert into user(userId,password,nickname,image,name,birthday,address,email,createtime,ispublic) " +
                     "values(?,?,?,?,?,?,?,?,?,'T')";
        int result = 0;
        try {
            result = this.executeUpdate(sql, user.getUserId(),user.getPassword(),
                    user.getNickname(),user.getImage(),user.getName(),user.getBirthday(),
                    user.getAddress(),user.getEmail(), DateUtil.now());
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return result;
    }
}
